const { Op } = require("sequelize");

const seq = require('../db/seq');
const Cart = require('../model/cart');
const Goods = require('../model/goods');

const cart = Cart(seq);
const goods = Goods(seq);

class CartController {
    //添加购物车
    async addCart(ctx, next) {
        const { goods_id, goods_number } = ctx.request.body;
        const user_id = ctx.state.user.user_id;
        try {
            //查询购物车中是否已有该商品
            let res1 = await cart.findOne({
                where: {
                    [Op.and]: {
                        user_id,
                        goods_id,
                    }
                }
            });
            if (!res1) {
                //如果没有，将该商品加入用户购物车
                let res2 = await cart.create({ user_id: user_id, goods_id: goods_id, goods_number: goods_number })
                ctx.body = {
                    code: 0,
                    message: '添加购物车成功',
                    result: res2.dataValues
                }
            } else {
                //如果有，更新该商品的数量
                let res3 = await cart.update({ goods_number: goods_number }, {
                    where: {
                        [Op.and]: {
                            user_id,
                            goods_id,
                        }
                    }
                })
                ctx.body = {
                    code: 0,
                    message: '添加购物车成功'
                }
            }
        } catch (err) {
            console.log('添加购物车失败', err);
            return ctx.body = {
                error: '添加购物车失败'
            }
        }
    }

    //更新购物车
    async updateCart(ctx, next) {
        const { id, goods_number } = ctx.request.body;
        try {
            let res1 = await cart.findOne({
                where: { id: id }
            });
            if (res1) {
                let res2 = await cart.update({ goods_number: goods_number }, { where: { id: id } })
                ctx.body = {
                    code: 0,
                    message: '更新购物车成功',
                }
            } else {
                ctx.body = {
                    message: '购物车id不存在'
                }
            }
        } catch (err) {
            console.log('更新购物车失败', err);
            return ctx.body = {
                error: '更新购物车失败'
            }
        }

    }

    //删除购物车
    async deleteCart(ctx, next) {
        const { ids } = ctx.request.body;
        try {
            const res = cart.destroy({
                where: {
                    id: {
                        [Op.in]: ids,
                    }
                }
            })
            ctx.body = {
                code: 0,
                message: '删除购物车成功',
                result: res
            }
        } catch (err) {
            console.log('删除购物车失败', err);
            return ctx.body = {
                error: '删除购物车失败'
            }
        }

    }

    //获取购物车列表
    async getCart(ctx, next) {
        const user_id = ctx.state.user.user_id;
        try {
            const { count, rows } = await cart.findAndCountAll({
                attributes: ['id', 'goods_number', 'goods_id', 'user_id'],
                include: {
                    model: goods,
                    as: 'goods_info',
                    attributes: ['goods_id', 'goods_name', 'goods_price', 'goods_img'],
                },
                where: {
                    user_id: user_id
                }
            })
            ctx.body = {
                code: 0,
                message: '获取购物车列表成功',
                result: {
                    count: count,
                    rows: rows
                }
            }
        } catch (err) {
            console.log('获取列表失败', err);
            return ctx.body = {
                error: '获取购物车列表失败'
            }
        }

    }
}

module.exports = new CartController();